import axios from "axios";

//创建一个axios实例 初始化
const axiosInstance = axios.create({
    baseURL: 'http://localhost:46208',
    // timeout: 5000,
    headers: { 'X-Custom-Header': 'foobar', 'Authorization': 'kbPpDmex2xNn6eN47AJTp2FEncQHcaWp' }
})

//预设网络异常代码
const jsonHttpcode = {
    A100: 'Continue 继续',
    A101: 'Switching Protocols 切换协议',
    A400: 'Bad Request 客户端请求的语法错误',
    A401: 'Unauthorized 请求要求用户的身份认证',
    A403: 'Forbidden ',
    A404: 'Not Found'
}
//预设接口异常代码
const jsonApiCode = {
    Fail: 'This is fail',
    Error: 'This is error'
}
//函数封装
function reqFuncAxios(methodIn, urlIn, reqDataIn, FuncflowIn) {
    let reqAttribute = {
        url: urlIn,
        method: methodIn
    }
    switch (methodIn) {
        case 'put':
        case 'post':
            reqAttribute.data = reqDataIn;
            break;
        case 'delete':
        case 'get':
            reqAttribute.params = reqDataIn;
            break;
    }
    const promiseReq = axiosInstance(reqAttribute);  //调用axios实例请求后端接口
    promiseReq.then((reResult) => {
        if (reResult.status === 200) {
            if (reResult.data.StatusCode === "Success") {
                FuncflowIn(reResult.data)
            }
            else {
                alert(jsonApiCode[reResult.data.StatusCode])
            }
        }
        else {
            alert(jsonHttpcode['A' + reResult.status])
        }
    })
}

export default reqFuncAxios